import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import Controls from '#cpt/temp/Controls'
import TableControls from '#cpt/temp/TableControls'
import Confirm from '#cpt/temp/Confirm'
import AddEditModal from '#cpt/temp/AddEditModal'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== private component
const $cache 		= $Lazy.file(()=>import('#private/cache-api'))
const ExportModal 	= $Lazy.state(()=>import('./export'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		// 搜索表单列表
		forms : [
			{ label: '单位名称',		name: 'company_name' },
			{ label: '单位编码',		name: 'company_code' },
			{ label: '单位简称 ',	name: 'company_short_name' },
			{ label: '主检实验室',	name: 'company_laboratory' },
		],
		// 提交表单列表
		formData : [
			{ label: '单位名称',		name: 'company_name', 					half:true, required:true },
			{ label: '单位简称',		name: 'company_short_name', 			half:true, required:true },
			{ label: '联系人',		name: 'company_employee', 				half:true },
			{ label: '联系电话',		name: 'company_mobile', 				half:true },
			{ label: '地址',		name: 'company_address', 				half:true },
			{ label: '主检实验室',	name: 'company_laboratory', 			half:true },
			{ label: '导出模板',		name: 'company_export_template_code', 	half:true, type:'select',	idStr:'model_code',	nameStr:'model_name' },
		]
	}
	
	model = {}
	
	componentDidMount(){
		this.props.onRef?.(this)
		if(window.isInit){
			this.fetch()
		}
	}
	// 获取弹框下拉
	getModalSelect = async () => {
		const { formData } = this.state
		$fn.setModalRows(formData, 'company_export_template_code', ()=>{
			$cache.then( async f => {
				// 导出模板
				$fn.setRow(formData, 'company_export_template_code',  await f.getCache({ api:'export_template'}))
				//
				this.setState({ formData })
			})
		})
	}
	// paging
	fetch = () => $fn.fetch.call(this,'lis-outsourcing-company/index')
	
	// 表单
	cols = [
		{ title: '单位名称', 	dataIndex: 'company_name', 			width:150 },
		{ title: '单位编码', 	dataIndex: 'company_code', 			width:100 },
		{ title: '单位简称', 	dataIndex: 'company_short_name', 	width:150 },
		{ title: '联系人', 		dataIndex: 'company_employee', 		...$fn.table.name },
		{ title: '联系电话', 	dataIndex: 'company_mobile', 		...$fn.table.phone },
		{ title: '地址', 		dataIndex: 'company_address', 		width:200  },
		{ title: '主检实验室', 	dataIndex: 'company_laboratory', 	width:100 },
		{ title: '导出模板', 	dataIndex: 'model_name', 			width:150 },
		TableControls.controls.call(this,{ width: 180 })
	]
	// 表格操作按钮
	table_controls = (text,rows) => {
		return [
			TableControls.edit.call(this, null, null,{
				select: this.getModalSelect,
				rows
			}),																			// 编辑
			this.onDel(rows.uuid),														// 删除
			TableControls.click.call(this, '导出', ()=> this.modalexportmodal.open(rows) )							// 导出
		]
	}
	// 操作按钮
	controls = () => {
		return [
			Controls.add.call(this,{click:this.getModalSelect}),			// 添加
			this.onDel(),						// 删除
		]
	}
	// 删除
	onDel = uuid => Confirm.call(this, '删除', 'lis-outsourcing-company/del', { param: [ uuid ], isKeys: !uuid, idStr:'uuids' })
	
	render(){
		return (
			<>
				{/* 搜索 */}
				{ SearchForm.call(this) }
				{/* 表格 */}
				{ Table.call(this,{isInit:window.isInit}) }
				{/* 添加、编辑 */}
				{ AddEditModal.call(this,{ api: 'lis-outsourcing-company/add', editApi:'lis-outsourcing-company/edit' }) }
				{/* 账单信息 */}
				<ExportModal onRef={ref=>this.modalexportmodal=ref} />
			</>
		)
	}
}

export default Index